#include <httpd.h>
#include <http_protocol.h>
#include <http_config.h>
#include <http_log.h>

#include <apr_optional.h>
#include <apr_hash.h>

#define MAX_SIZE 1024

int N1 = 0, C1 = 0, C2 = 0;
int R = 0, N2 = 0;

/* Parse form data from a string*/
static apr_hash_t *parse_form_from_string(request_rec *r, char *args)
{
	apr_hash_t *form;
	apr_array_header_t *values;
	char *pair;
	char *eq;
	const char *delim = "&" ;
	char *last;
	char **ptr;
	if ( args == NULL) {
		return NULL;
	}
	form = apr_hash_make(r->pool) ;
	/* Split the input on '&' */
	for ( pair = (char *)apr_strtok(args, delim, &last) ; pair != NULL;
		pair = (char *)apr_strtok(NULL, delim, &last) ) {
		for ( eq = pair; *eq; ++eq) {
		if (*eq == '+' ) {
		*eq = ' ' ;
		}
	}
	eq = strchr(pair, '=' ) ;
	if (eq) {
		*eq++ = '\0' ;
		ap_unescape_url( pair) ;
		ap_unescape_url( eq) ;
	}
	else {
		eq = " " ;
		ap_unescape_url( pair) ;
	}
	/* Store <key,value> pair in our form hash.*/
	 ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
                        "->%s->%s",pair,eq);
	values = apr_hash_get(form, pair, APR_HASH_KEY_STRING) ;
	if (values == NULL) {
		values = apr_array_make(r->pool, 1, sizeof(const char*) ) ;
		apr_hash_set(form, pair, APR_HASH_KEY_STRING, values) ;
	}
	ptr = apr_array_push(values) ;
	*ptr = (char *)apr_pstrdup(r->pool, eq) ;
	}
	return form;
}

static apr_hash_t* parse_form_from_GET(request_rec *r)
{
	return (apr_hash_t *) parse_form_from_string(r, r->args);
}


static int idenman_handler(request_rec *r)
{
	apr_hash_t *formdata = NULL;
	int rv = OK;

	if (! r->handler || (strcmp(r->handler, "idenman") != 0) ) {
		return DECLINED;
	}	
	if ((r->method_number != M_GET) && (r->method_number != M_POST)) {
		return HTTP_METHOD_NOT_ALLOWED;
	}
	
	ap_set_content_type( r, " text/html; charset=ascii") ;
		
	/* Displaying the form data */
	if (r->method_number == M_GET) {
		formdata = (apr_hash_t *) parse_form_from_GET(r) ;
	}
	
	if (rv != OK) {
		ap_rputs("Error reading form data!" , r) ;
	}
	else if (formdata == NULL) {
		ap_rputs("No form data found." , r) ;
	}
	else {
		/* Parsed the form successfully*/
		apr_array_header_t *arr;
		char *key;
		apr_ssize_t klen;
		apr_hash_index_t *index;
		char **val;
		char *p;
		char *data;
		int i = 0, array[3];
		for (index = apr_hash_first(r->pool, formdata); index != NULL;
			index = apr_hash_next(index)) {
			apr_hash_this(index, (const void**) &key, &klen, (void**) &arr);
			val = apr_array_pop(arr);
			array[i++] = atoi(*val);
			ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,"%d",array[i-1]);
			/*ap_rprintf(r, "%s\n",
				ap_escape_html(r->pool, *val)) ;*/
		}
		C1 = array[0];
		C2 = array[1];
		N1 = array[2];
		ap_rprintf(r, "N1: %d\n",N1);
		ap_rprintf(r, "C1: %d\n",C1);
		ap_rprintf(r, "C2: %d\n",C2);
		srand(time(NULL));
		R = rand()%100;
		N2 = rand()%100;
		ap_rprintf(r, "r: %d\n",R);
		ap_rprintf(r, "N2: %d\n",N2);
		ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,"%d",R);		
	}
	return OK ;
}


static void idenman_hooks(apr_pool_t *pool)
{
	ap_hook_handler(idenman_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

module AP_MODULE_DECLARE_DATA idenman_module = {
	STANDARD20_MODULE_STUFF,
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	idenman_hooks
};
